package com.study4j.oss.fastdfs.util;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.study4j.common.exception.ServerException;

import lombok.extern.slf4j.Slf4j;

/**
 * @description Fastdfs工具类
 * @author Jiangbin
 * @date 2019年7月28日 下午6:07:07
 */
@Slf4j
@Component
@ConditionalOnProperty(prefix = "oss", name = "type", havingValue = "fastdfs")
public class FastdfsClientUtil {

	@Autowired
	private FastFileStorageClient storageClient;

	@Autowired
	private ThumbImageConfig thumbImageConfig;

	@PostConstruct
	private void check() {
		log.info("------------------检查与fastclien连接状况 start------------------");
		if (storageClient == null) {
			log.error("未连接到fastfile的storageClient，请检查配置文件中相关配置属性是否正确！");
			throw new ServerException("未连接到fastfile的storageClient");
		}
		log.info("												\r\n"
				+ "       ┌─┐       ┌─┐   						\r\n"
				+ "    ┌──┘ ┴───────┘ ┴──┐						\r\n"
				+ "    │                 │						\r\n"
				+ "    │       ───       │						\r\n"
				+ "    │  ─┬┘       └┬─  │						\r\n"
				+ "    │                 │						\r\n"
				+ "    │       ─┴─       │						\r\n"
				+ "    │                 │						\r\n"
				+ "    └───┐         ┌───┘						\r\n"
				+ "        │         │							\r\n"
				+ "        │         │							\r\n"
				+ "        │         │							\r\n"
				+ "        │         └──────────────┐			\r\n"
				+ "        │                        │			\r\n"
				+ "        │                        ├─┐			\r\n"
				+ "        │                        ┌─┘			\r\n"
				+ "        │                        │			\r\n"
				+ "        └─┐  ┐  ┌───────┬──┐  ┌──┘			\r\n"
				+ "          │ ─┤ ─┤       │ ─┤ ─┤				\r\n"
				+ "          └──┴──┘       └──┴──┘				\r\n"
				+ "                 神兽保佑						\r\n"
				+ "                代码无BUG!					\r\n" + "-------------检查与fastclien连接状况  end-------------");
	}

	/**
	 * @description 上传文件
	 * @param file 需要上传的文件
	 * @return
	 * @throws Exception
	 * @author Jiangbin
	 * @date 2019年7月26日 下午3:10:20
	 */
	public String upload(MultipartFile file) throws Exception {
		// 文件名
		String originalFilename = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
		// 文件扩展名
		String extendName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1,
				originalFilename.length());
		StorePath storePath;
		storePath = this.storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
				originalFilename, null);
		String path = storePath.getFullPath();
		return path;

	}

	/**
	 * @description 删除文件
	 * @param fileUrl 文件访问地址
	 * @author Jiangbin
	 * @date 2019年7月26日 下午3:11:35
	 */
	public void deleteFile(String fileUrl) {
		if (StringUtils.isEmpty(fileUrl)) {
			return;
		}
		try {
			StorePath storePath = StorePath.parseFromUrl(fileUrl);
			storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
		} catch (FdfsUnsupportStorePathException e) {
			log.error(e.getMessage());
			throw e;
		}
	}

}
